package Q541;

/**
 * 反转字符串2
 * @author 23737
 * @time 2021.11.26
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(new Solution().reverseStr("abcdefg", 2));
    }
}

class Solution {
    public String reverseStr(String s, int k) {
        StringBuilder res = new StringBuilder();
        int index = 0;
        int length = s.length();
        while (index < length){
            int end = Math.min(index+k,length);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(s.substring(index,end));
            if((index/k) % 2 == 0){
                res.append(stringBuilder.reverse());
            }else {
                res.append(stringBuilder);
            }
            index = end;
        }
        return res.toString();
    }
}
